package org.itboys.core.service.mongo.web;

import org.itboys.commons.utils.tools.LongUtil;
import org.itboys.core.common.SundayCommonConstants;
import org.itboys.core.entity.mongo.SundayProductLabel;
import org.itboys.core.service.mysql.front.SundayFrontProductService;
import org.itboys.mongodb.core.MongoDataSource;
import org.itboys.mongodb.service.BaseMongoService;
import org.itboys.param.PageResult;
import org.itboys.param.ParamUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.Map;

/**
 * Auther: 罗尧坤
 * Date: 2018/8/22
 * Description:商品标签服务类
 */

@Service
public class SundayProductLabelService extends BaseMongoService<SundayProductLabel> {
    @Resource(name = "mobileDs")
    private MongoDataSource mongoDataSource;
    @Override
    protected MongoDataSource getMongoDataSource() {
        return mongoDataSource;
    }
    @Override
    protected Class<SundayProductLabel> getEntityClass() {
        return SundayProductLabel.class;
    }

    @Autowired
    private SundayFrontProductService productService;

    /**
     * 分页查询所有的标签
     * @param request
     * @return
     */
    public PageResult<SundayProductLabel> select(HttpServletRequest request){
        Map<String, Object> param = ParamUtil.packageMongoExactParam(request);
        //模糊查询条件
        Map<String, String> contain = ParamUtil.packageMongoVagueParam(request);

        param.put("isDeleted", SundayCommonConstants.NO);

        return super.containpageQuery(request,contain,param);
    }

    /**
     * 新增或者修改标签
     * @param id
     * @param label
     */
    public void save(Long id, SundayProductLabel label){
        if (LongUtil.isNotZreo(id)){
            super.updateExceptEmpty(id, label);
        }else {
            super.save(label);
        }
    }

    /**
     * 查找单个标签
     * @param id
     * @return
     */
    public SundayProductLabel findOne(Long id) {
        SundayProductLabel label = new SundayProductLabel();

        if (LongUtil.isNotZreo(id)) {
            label = super.getById(id);
            return label;
        } else {
            return label;
        }
    }

}
